package org.hdax.dm.service.impl;

import org.aspectj.weaver.ast.ITestVisitor;
import org.hdax.dm.client.DmImageClient;
import org.hdax.dm.client.DmItemClient;
import org.hdax.dm.client.DmItemTypeClient;
import org.hdax.dm.entity.image.DmImage;
import org.hdax.dm.entity.item.DmItem;
import org.hdax.dm.entity.item_type.DmItemType;
import org.hdax.dm.service.DmItemTypeService;
import org.hdax.dm.vo.CommonResponse;
import org.hdax.dm.vo.VoUtil;
import org.hdax.dm.vo.entity.item.VoDmItem;
import org.hdax.dm.vo.entity.item.VoDmItemTwo;
import org.hdax.dm.vo.entity.item_type.VoDmItemType;
import org.hdax.dm.vo.entity.item_type.VoDmItemTypeTwo;
import org.hdax.dm.vo.entity.item_type.VoDmItemTypethree;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DmItemTypeServiceImpl implements DmItemTypeService {
    @Autowired
    private DmItemTypeClient dmItemTypeClient;

    @Autowired
    private DmItemClient dmItemClient;

    @Autowired
    private DmImageClient dmImageClient;

    @Override
    public CommonResponse<List<VoDmItemType>> queryAllType() {
        //用来存储变量出的一级菜单
        List<VoDmItemType> voDmItemTypeList = new ArrayList<>();
        //获取到所有菜单
        List<DmItemType> dmItemTypes = dmItemTypeClient.findAll();


        //获取一级菜单
        dmItemTypes.forEach(item -> {
            VoDmItemType voDmItemType = new VoDmItemType();//向客户端返回的一二级菜单的数据进行替换
            BeanUtils.copyProperties(item, voDmItemType);
            //判断是否是一级菜单
            if (item.getParent() == 0 && item.getLevel().equals("1")) {
                //遍历一级菜单的热门推荐
                List<DmItem> dmItemList = dmItemClient.findByDmitem(0, 5, new DmItem(item.getId(), 1));
                dmItemList.forEach(items -> {
                    if (items != null && item.getId() == items.getItemType1Id()) {
                        //向客户端返回的节目数据进行替换
                        VoDmItem voDmItem = new VoDmItem();
                        List<DmImage> dmImages = dmImageClient.findByImage(new DmImage(items.getId(), 2, 1));
                        if (dmImages.size() > 0) {
                            voDmItem.setImgUrl(dmImages.get(0).getImgUrl());
                        }
                        BeanUtils.copyProperties(items, voDmItem);
                        voDmItemType.getHotItems().add(voDmItem);
                    }
                });
                voDmItemTypeList.add(voDmItemType);
            }
        });

        //根据遍历出的一级菜单，获取二级菜单
        voDmItemTypeList.forEach(item -> {
            //循环集合查询二级
            dmItemTypes.forEach(dm -> {
                //判断一级菜单是否是自己的父级菜单
                if (dm.getLevel().equals("2") && dm.getParent() > 0 && item.getId() == dm.getParent()) {
                    //向客户端返回的一二级菜单的数据进行替换
                    VoDmItemType voDmItemType = new VoDmItemType();
                    BeanUtils.copyProperties(dm, voDmItemType);
                    item.getChildren().add(voDmItemType);
                }
            });
        });
        return VoUtil.returnSuccess(voDmItemTypeList);
    }

    @Override
    public CommonResponse<List<VoDmItemTypeTwo>> queryTransverse() {
        List<DmItemType> dmItemTypeList = dmItemTypeClient.findByParent(0);
        List<VoDmItemTypeTwo> voDmItemTypeTwoList = new ArrayList<>();

        dmItemTypeList.forEach(item -> {
            VoDmItemTypeTwo voDmItemTypeTwo = new VoDmItemTypeTwo();
            BeanUtils.copyProperties(item, voDmItemTypeTwo);
            voDmItemTypeTwoList.add(voDmItemTypeTwo);
        });
        return VoUtil.returnSuccess(voDmItemTypeTwoList);
    }

    @Override
    public CommonResponse<List<VoDmItemTwo>> queryHotItems(Integer itemTypeId) {
        List<VoDmItemTwo> voDmItemTwoList = new ArrayList<>();
        //遍历一级菜单的热门推荐
        List<DmItem> dmItemList = dmItemClient.findByDmitem(0, 12, new DmItem(itemTypeId, 1, new Object()));
        dmItemList.forEach(item -> {
            VoDmItemTwo voDmItemTwo = new VoDmItemTwo();
            voDmItemTwo.setId(item.getId());
            voDmItemTwo.setItemName(item.getItemName());
            voDmItemTwoList.add(voDmItemTwo);
        });
        return VoUtil.returnSuccess(voDmItemTwoList);
    }

    @Override
    public CommonResponse<List<VoDmItemTypethree>> queryItemType(Integer parent) {
        List<VoDmItemTypethree> voDmItemTypethreeList = new ArrayList<>();
        List<DmItemType> dmItemTypeList = dmItemTypeClient.findByParent(parent);
        if (dmItemTypeList.size() > 0) {
            dmItemTypeList.forEach(item -> {
                VoDmItemTypethree voDmItemTypethree = new VoDmItemTypethree();
                BeanUtils.copyProperties(item, voDmItemTypethree);
                if (item.getParent() != null && item.getParent() > 0) {
                    voDmItemTypethree.setLevel(2);
                } else {
                    voDmItemTypethree.setLevel(1);
                }
                voDmItemTypethreeList.add(voDmItemTypethree);
            });
        }
        return VoUtil.returnSuccess(voDmItemTypethreeList);
    }
}
